﻿//1. 客户方程序 QuoteClient.java
package javase.socket.client;
import java.io.*;
import java.net.*;
import java.net.DatagramPacket;
import java.util.*;
public class QuoteClient {
   public static void main(String[] args) throws IOException 
   { 
      if(args.length!=1) { 
      //如果启动的时候没有给出Server的名字，那么出错退出
       System.out.println("Usage:java QuoteClient <hostname>"); 
       //打印出错信息
       return;
       //返回
     }
     DatagramSocket socket=new DatagramSocket(); 
     //创建数据报套接字

     //Byte[] buf=new Byte[256]; //创建缓冲区
     InetAddress address=InetAddress.getByName(args [0]); 
     //由命令行给出的第一个参数默认为Server的名字，通过它得到Server的IP信息
     byte [] buf=new byte[1024];
     DatagramPacket packet=new DatagramPacket(buf,buf.length,address,4446);
     //创建DatagramPacket对象
     socket.send(packet);
     packet=new DatagramPacket(buf,buf.length); 
     //创建新的DatagramPacket对象，用来接收数据报
     socket.receive(packet); //接收
     String received=new String(packet.getData());
     //根据接收到的字节数组生成相应的字符串
     System.out.println("Quote of the Moment:"+received ); 
     //打印生成的字符串
     socket.close(); //关闭套接口
     }
     }

